<?php
/**
 * Created by PhpStorm.
 * User: 69571
 * Date: 2019/8/14
 * Time: 16:18
 */
namespace app\models;
use Yii;
use yii\base\Model;

class Api extends  Model
{
    /**
     * @return array
     *  查询所有的商品类型
     */
    public  function types(){
        $types = (new \yii\db\Query())
            ->select('*')
            ->from('type')
            ->where(['type_pid'=>0])
            ->all();
        return $types;
    }
    /**
     * @return array
     * 商品类型查询
     * $type_id 为查询的条件。
     */
    public function shops($type_id){
        $shop_arr  = (new \yii\db\Query())
            ->select('*')
            ->from('shop')
            ->where(['shop_type'=>$type_id])
            ->all();
        return $shop_arr;
    }
    /**
     * @param $id
     * @return array
     *  商品详情查看
     */
    public function shopOne($id){
        $shop_one = (new \yii\db\Query())
            ->select('*')
            ->from('shop')
            ->where(['shop_id'=>$id])
            ->all();
        if($shop_one){
            return $shop_one;
        }else{
            return array(['code'=>500,'msg'=>'您查询的该条数据为空']);
        }
    }
    /**
     * @param $id
     * @return array|bool
     *  当点击要加入的物品时，将
     * 该商品的id 加入到 购物车关联表中，默认将其支付状态值设置为0
     */
    public function AddShopCar($id){
        $car_res = Yii::$app->db->createCommand("INSERT INTO shop_car (shop_id) VALUES ($id)")->execute();
        if($car_res){
            $car_arr = Yii::$app->db->createCommand(" SELECT * FROM shop_car as c join shop as s on c.shop_id =s.shop_id WHERE c.pay_status = 0")->queryAll();
        }
        return $car_arr;
    }
    /**
     * @param $ids
     * @return array
     * @throws \yii\db\Exception
     *  计算购物车中 所有商品的价格
     */
    public function payCar($ids){
        $pay_arr = Yii::$app->db->createCommand(" SELECT sum(s.shop_showprice) as priceSum FROM shop_car as c join shop as s on c.shop_id =s.shop_id WHERE c.shop_id in ($ids)")->queryAll();
        return $pay_arr;
    }



    public function addRess($data){

    }




























}